<template>
  <div>
    <nav-bar :title="title" :isShow="false"></nav-bar>
    <van-cell title="律师/销售登录" size="large" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import NavBar from "@/components/NavBar";
import { login } from "@/api/user";
import { setToken } from "@/utils/auth";
export default {
  components: {
    NavBar,
  },
  props: {},
  data() {
    return {
      title: this.$route.meta.title,
      username: "",
      password: "",
    };
  },
  computed: {},
  methods: {
    onSubmit(values) {
      console.log("submit:", values);
      this.user_login(values);
    },
    user_login(data) {
      login(data)
        .then((res) => {
          const data = res.data;
          if (data.status_code == 200) {
            setToken(data.token, data.expires);
            this.$dialog
              .alert({
                title: "登录提示",
                message: "您已经成功登录",
              })
              .then(() => {
                this.$router.replace("/case");
              });
          } else {
            this.$notify({
              type: "danger",
              message: "获取token失败",
            });
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$toast(error.response.data.message);
            this.$notify({
              type: "danger",
              message: error.response.data.message,
            });
            // console.log("a:", error.response.data);
          } else {
            console.log("Error", error.message);
          }
        });
    },
  },
};
</script>

<style  scoped>
</style>